package org.example.managere.controller;

import jakarta.validation.Valid;
import org.example.managere.dto.FMEAFMESDto;
import org.example.managere.service.FMEAFMESService;
import org.example.managere.util.AdvancedSearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/fmeafmes")
public class FMEAFMESController {

    private final FMEAFMESService fmeafmesService;

    @Autowired
    public FMEAFMESController(FMEAFMESService fmeafmesService) {
        this.fmeafmesService = fmeafmesService;
    }

    @GetMapping
    public ResponseEntity<List<FMEAFMESDto>> getAllFMEAFMES() {
        return ResponseEntity.ok(fmeafmesService.getAllFMEAFMES());
    }

    @GetMapping("/page")
    public ResponseEntity<Page<FMEAFMESDto>> getFMEAFMESWithPagination(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "asc") String direction) {
        
        Sort.Direction sortDirection = direction.equalsIgnoreCase("desc") ? 
                Sort.Direction.DESC : Sort.Direction.ASC;
        
        Pageable pageable = PageRequest.of(page, size, Sort.by(sortDirection, sortBy));
        return ResponseEntity.ok(fmeafmesService.getFMEAFMESWithPagination(pageable));
    }

    @GetMapping("/search")
    public ResponseEntity<Page<FMEAFMESDto>> searchFMEAFMES(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        Pageable pageable = PageRequest.of(page, size);
        return ResponseEntity.ok(fmeafmesService.searchFMEAFMES(keyword, pageable));
    }

    @GetMapping("/advanced-search")
    public ResponseEntity<Page<FMEAFMESDto>> advancedSearch(
            @RequestParam Map<String, String> searchParams,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String sort) {
        
        // 构建分页和排序
        Pageable pageable;
        if (sort != null && !sort.isEmpty()) {
            List<Sort.Order> orders = AdvancedSearchUtil.buildSortOrders(sort);
            pageable = PageRequest.of(page, size, Sort.by(orders));
        } else {
            pageable = PageRequest.of(page, size);
        }
        
        return ResponseEntity.ok(fmeafmesService.advancedSearch(searchParams, pageable));
    }

    @GetMapping("/{id}")
    public ResponseEntity<FMEAFMESDto> getFMEAFMESById(@PathVariable Integer id) {
        return ResponseEntity.ok(fmeafmesService.getFMEAFMESById(id));
    }

    @PostMapping
    public ResponseEntity<FMEAFMESDto> createFMEAFMES(@Valid @RequestBody FMEAFMESDto fmeafmesDto) {
        return new ResponseEntity<>(fmeafmesService.createFMEAFMES(fmeafmesDto), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<FMEAFMESDto> updateFMEAFMES(
            @PathVariable Integer id, 
            @Valid @RequestBody FMEAFMESDto fmeafmesDto) {
        return ResponseEntity.ok(fmeafmesService.updateFMEAFMES(id, fmeafmesDto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFMEAFMES(@PathVariable Integer id) {
        fmeafmesService.deleteFMEAFMES(id);
        return ResponseEntity.noContent().build();
    }
} 